package com.ccsu.joysport.controller;

import com.ccsu.joysport.entity.Results;
import com.ccsu.joysport.entity.Target;
import com.ccsu.joysport.service.ResultService;
import com.ccsu.joysport.service.TargetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;



@RestController
@RequestMapping("/ccsu/admin/target")
public class TargetController {
    @Autowired
    private TargetService targetService;

    @Autowired
    private ResultService resultService;

    @GetMapping
    public List<Target> getAllTargets() {
        return targetService.getAllTargets();
    }

    @GetMapping("/{targetID}")
    public Target getTargetById(@PathVariable Long targetID) {
        return targetService.getTargetById(targetID);
    }

    @PostMapping
    public Target createTarget(@RequestBody Target target) {
        return targetService.createTarget(target);
    }

    @PutMapping("/{targetID}")
    public Target updateTarget(@PathVariable Long targetID, @RequestBody Target targetDetails) {
        return targetService.updateTarget(targetID, targetDetails);
    }

    @DeleteMapping("/{targetID}")
    public void deleteTarget(@PathVariable Long targetID) {
        targetService.deleteTarget(targetID);
    }

    @GetMapping("/suggestion")
    public ResponseEntity<String> getSuggestionByToken(@RequestParam String token) {
        // 打印获取到的token
        System.out.println("Received token: " + token);

        List<Results> results = resultService.getResultsByToken(token);
        if (results != null && !results.isEmpty()) {
            Results result = results.get(0); // 假设只取第一个结果

            // 打印从results获得的weight和height
            System.out.println("Weight from results: " + result.getWeight());
            System.out.println("Height from results: " + result.getHeight());

            double heightCm = Double.parseDouble(result.getHeight());
            double weight = Double.parseDouble(result.getWeight());

            // 打印解析后的height和weight
            System.out.println("Parsed height (cm): " + heightCm);
            System.out.println("Parsed weight: " + weight);

            String suggestion = targetService.getSuggestionByBmi(heightCm, weight);
            return ResponseEntity.ok(suggestion);
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("No results found for the given token.");
        }
    }
}
